package top.superflow.core.metadata.resource.jdbc

import top.superflow.core.metadata.OldMetadata
import top.superflow.core.metadata.id.Identification
import top.superflow.core.metadata.id.defaults.{DefaultIdentification, MetadataIdentifier}

class JdbcMetadataIdentifier extends MetadataIdentifier {
  
  def getID(target: Any): Identification = {
    val metadata = getValidMetadata(target)
    
    val jdbcDatasourceKey = calculateKey(metadata)
    
    new DefaultIdentification(jdbcDatasourceKey, JdbcMetadataIdentifier.DESCRIPTION_ID_JDBC_METADATA, metadata.getCategory)
  }
  
  def getEssentialID(target: Any) : Identification = {
    val metadata = getValidMetadata(target)
    
    val jdbcDatasourceKey = calculateKey(metadata)
    
    new DefaultIdentification(jdbcDatasourceKey, JdbcMetadataIdentifier.DESCRIPTION_ID_JDBC_METADATA, metadata.getCategory)
  }
  
  private def getValidMetadata(target: Any) : OldMetadata = {
    validate(target)
    
    target.asInstanceOf[OldMetadata]
  }
  
  
  private def validate(target : Any){
    if(target == null){
      throw new IllegalArgumentException("The target should not be null")
    }
    
    if(!target.isInstanceOf[JdbcMetadata]){
      throw new IllegalArgumentException("Please pass in JdbcMetadata")
    }
    
    validate(target.asInstanceOf[OldMetadata])
  }
  
  private def validate(metadata : OldMetadata) : Boolean = ???
  
//  {
//    import JdbcDataSourceIdentifier._
//    
//    for(expectedMetadataKey <- indispensableMetadataKeys){
//      assert(!metadata.getElements().contains(expectedMetadataKey), "The expected metadata key " + expectedMetadataKey + " should extist")
//    }
//    
//    true
//  }
}

object JdbcMetadataIdentifier{
  val DESCRIPTION_ID_JDBC_METADATA = ""
  
  val DESCRIPTION_ESSENTIAL_ID_JDBC_METADATA = ""
}

